#include "main.h"
#include "system.h"
#include	"BSP_LoRa.h"

void SetLoRaModeToWorkClassC(void)
{
	uint8 i = 0, class_a_mode = 0;
	LoRo_Uart_CleanData();
	LoRa_Uart_Send("AT+CLASS?\r\n", 11);
	i = 10;
	while (i--)
	{
		NB_Delay(2000);
		if (gNB_ReceData->len>0)
		{
			gNB_ReceData->buf[gNB_ReceData->len]=0;
			printf("R--->>%s\n",gNB_ReceData->buf);		
			if (strstr(gNB_ReceData->buf, "OK+CLASS:00\r\n") != NULL)
			{
				printf("lora is CLASS A mode\n");
				class_a_mode = 1;
				break;
			}
			else if (strstr(gNB_ReceData->buf, "OK+CLASS:01\r\n") != NULL)
			{
				printf("lora is CLASS C mode\n");
				class_a_mode = 0;
				break;
			}
		}
		
	}
	
	if (class_a_mode == 1)
	{
		LoRo_Uart_CleanData();
		LoRa_Uart_Send("AT+CLASS=01\r\n", 13);
		i = 10;
		while (i--)
		{
			if(i==5)
			{
				LoRo_Uart_CleanData();
				LoRa_Uart_Send("AT+CLASS=01\r\n", 13);

			}
			NB_Delay(2000);
			if (gNB_ReceData->len>0)
			{

				gNB_ReceData->buf[gNB_ReceData->len]=0;
				printf("R--->>%s\n",gNB_ReceData->buf); 	
		
 				if (strstr(gNB_ReceData->buf, "OK+CLASS\r\n") != NULL)
				{
	
					gNB_ReceData->buf[gNB_ReceData->len]=0;
					printf("R--->>%s\n",gNB_ReceData->buf); 	
					LoRo_Uart_CleanData();
					LoRa_Uart_Send("AT+SAVE\r\n", 9);
					NB_Delay(2000);
					gNB_ReceData->buf[gNB_ReceData->len]=0;
					printf("R--->>%s\n",gNB_ReceData->buf); 

					break;
				}
				else
				{
					printf("set class c fail\n");
				}
			}
			
		}
	}
}
#if 0

uint8 WaitForLoRaReceiveData(uint8 ReceiveTerminatorTime)
{
	uint16 i = 0;
	while (gNB_ReceData->len>0)
	{
		if (++i >= 20 * ReceiveTerminatorTime)
		{
			break;
		}
		NB_Delay(250);
	}
	
	if (gLoRaRxFlag)
	{
		gLoRaRxFlag = 0;									  
		LoRo_Uart_Receive((void *)&gNB_ReceData->buf); 
		return SUCCESS;
	}
	return ERROR;
}
#endif
/************************************************
R>>

OK+SENT:01
OK+RECV:02,00,00
OK+RECV:00,02,0a,b0b00202584301f402f4
-->>Process CMD
TYPE=2
PORT=0
LEN=0
-->>Pay Attention
TYPE=0
PORT=2
LEN=10
data=b0b00202584301f402f4

crc right
cmd=2
UploadInterval=600
TemperThreshold=670
CurrentThreshold=500
DefaultPara_Flag=5aa5
---Sys flash write RIGHT
---UploadInterval=600
---TemperThreshold=670
---CurrentThreshold=500

--send--
b0 b0 03 00 02 58 9e 01 f4 03 50 
---
<<---T-36:---AT+SEND=1,4,B0B0030002589E01F40350

***gLoRaRxFlag=3
R>>OK+SEND:0b
OK+SENT:02
OK+RECV:02,00,00

send data length=0b
send data success
************************************************/


uint8 ReceiveCmdFromServer(uint8*Recebuffer)
{
	uint8 temp = 0;
	uint8 datalength = 0;
	uint8 *start, *next, i = 0, j = 0;
	uint8		buf[100]={0};
	
	if(gNB_ReceData->len>0)
	{
		gNB_ReceData->buf[gNB_ReceData->len]=0;
		printf("R--->>%s\n",gNB_ReceData->buf); 	
		
		

			
			
				
				
				#if 1
				start = strstr(gNB_ReceData->buf, "OK+RECV:");
				if (start != NULL)
				{
						memcpy(buf, start + strlen("OK+RECV:"), 2);
						buf[2] = 0;
						printf("TYPE=%d\n", ConvertStringToHex(buf));
				}
				else
				{
					gNB_ReceData->len=0;
					return ErrCode_Success;
				}

				start = strstr(gNB_ReceData->buf, ",");
				if (start != NULL)
				{
						memcpy(buf, start + strlen(","), 2);
						buf[2] = 0;
						printf("PORT=%d\n", ConvertStringToHex(buf));
				}

				next = strstr(start + strlen(","), ",");
				if (start != NULL)
				{
					memcpy(buf, next + strlen(","), 2);
					buf[2] = 0;
					printf("LEN=%d\n", ConvertStringToHex(buf));

					datalength = ConvertStringToHex(buf);
				}

				if (datalength > 0)
				{
					memset(buf, 0, sizeof(buf));
					start = strstr(next + strlen(","), ",");
					if (start != NULL)
					{
						memcpy(buf, start + strlen(","), datalength * 2); 

						printf("data=%s\n", buf);
					}
					i = 0;
					j = 0;
					for (i = 0; i < datalength * 2; i = i + 2)
					{
						Recebuffer[j++] = ConvertStringToHex(buf + i); 
					}
					if ((Recebuffer[0]==0xb0)&&(Recebuffer[1]==0xb0))
					{
						gNB_ReceData->len=0;
						return ErrCode_ReceData;
					}
					else
					{
						gNB_ReceData->len=0;
						return ErrCode_Success;	
					}
				}
				#endif
				gNB_ReceData->len=0;
				return ErrCode_Success;
			
		

		#if 0
		else if (strstr(gNB_ReceData->buf, "ERR+SEND:") != NULL) 
		{
			start = strstr(gNB_ReceData->buf, "ERR+SEND:");
			if (start != NULL)
			{
				memcpy(buf, start + strlen("ERR+SEND:"), 2);
				buf[2] = 0;
				temp = ConvertStringToHex(buf);
			}

			switch (temp)
			{
			case 0:
				printf("not join net\n");
				break;
			case 1:
				printf("busy!\n");
				gNB_ReceData->len=0;
				return ErrCode_Busy;
			case 2:
				printf("crc err!\n");
				break;
			case 3:
				printf("To long!\n");
				break;
			default:
				break;
			}
		}
		#endif
	}
	gNB_ReceData->len=0;
	printf("receive fail\n");
	return ErrCode_Fail;
}


uint8 JudegWhetherSendingSuccessful(void)
{
	uint8 temp = 0;
	uint8 datalength = 0;
	uint8 *start, *next, i = 0, j = 0;
	uint8		buf[100]={0};
	NB_Delay(4000);
	if(gNB_ReceData->len>0)
	{
		gNB_ReceData->buf[gNB_ReceData->len]=0;
		printf("R--->>%s\n",gNB_ReceData->buf); 	
		if (strstr(gNB_ReceData->buf,"OK+SEND:") != NULL)
		{

			if (strstr(gNB_ReceData->buf, "OK+SENT:") != NULL)
			{
				printf("send data success\n");
				return ErrCode_Success;
			}
		}
		else if (strstr(gNB_ReceData->buf, "ERR+SEND:") != NULL) 
		{
			start = strstr(gNB_ReceData->buf, "ERR+SEND:");
			if (start != NULL)
			{
				memcpy(buf, start + strlen("ERR+SEND:"), 2);
				buf[2] = 0;
				temp = ConvertStringToHex(buf);
			}

			switch (temp)
			{
			case 0:
				printf("not join net\n");
				break;

			case 1:
				printf("busy!\n");
				return ErrCode_Busy;
			case 2:
				printf("crc err!\n");
				break;
			case 3:
				printf("To long!\n");
				break;
			default:
				break;
			}
		}
	}
	printf("send fail\n");
	return ErrCode_Fail;
}

uint8 LoRaSendDada(CollectedData*value)
{
	uint8 i = 0, errcode,j=0;
	uint16 crc;
	
	uint8 	sendbuffer[36] = {		
						   0xb0, 0Xb0,		
						   0X01,			 
						   0x00,0x00,0x00,0x00,  
						   0x00,0x00,0x00,0x00, 0x00,0x00,0x00, 
						   0x00,0x00,0x00,0x00, 0x00,0x00,0x00, 
						   0x00,0x00,0x00,0x00, 0x00,0x00,0x00, 
						   0x00,0x00,0x00,	
						   0x00,0x00,		
						   0x00,			
						   0x00, 0x00		};
	uint8 	receivebuf[100]={0};

	
	
	if(SysPara.ChannleStatus.CurChannle_a==Channle_Status_Open) 
	{
		if(value->CurData.aVal>SysPara.thresholdVal.CurThreshold_a)
		{
			sendbuffer[4-1]|=Cur_Warn_OverCurrent<<Current_Shift_Val;
		}
		else  if(value->CurData.aVal>Current_Zero_Point_abc)
		{
			sendbuffer[4-1]|=Cur_Warn_Normal<<Current_Shift_Val;
			
		}
		else
		{
			sendbuffer[4-1]|=Cur_Warn_NoSensor<<Current_Shift_Val; 
		}		

	}
	else
	{
		sendbuffer[4-1]|=Cur_Warn_Normal<<Current_Shift_Val;
	}

	if(SysPara.ChannleStatus.VolChannle_a==Channle_Status_Open)
	{
		if(value->VolData.aVal>SysPara.thresholdVal.Vol_OverThreshold_a)
		{
			sendbuffer[4-1]|=Vol_Warn_OverVoltage<<Voltage_Shift_Val;
		}
		else	if(value->VolData.aVal>SysPara.thresholdVal.Vol_BelowThreshold_a)
		{
			sendbuffer[4-1]|=Vol_Warn_Normal<<Voltage_Shift_Val;
		}
		else	if( value->VolData.aVal>Voltage_Zero_Point)
		{
			sendbuffer[4-1]|=Vol_Warn_UnderVoltage<<Voltage_Shift_Val;
		}
		else 
		{
			sendbuffer[4-1]|=Vol_Warn_CutOFF<<Voltage_Shift_Val;
		}
	}
	else 
	{
		sendbuffer[4-1]|=Vol_Warn_Normal<<Voltage_Shift_Val;
	}

	if(SysPara.ChannleStatus.TemChannle_a==Channle_Status_Open) 
	{
		if( value->TemData.aVal/10>SysPara.thresholdVal.TemThreshold_a)
		{	
			sendbuffer[4-1]|=Tem_Warn_OverHigh<<Temperature_Shift_Val;
		}
		else	if( value->TemData.aVal>0)
		{
			sendbuffer[4-1]|=Tem_Warn_Normal<<Temperature_Shift_Val;
		}
		else
		{
			
			sendbuffer[4-1]|=Tem_Warn_NoSensor<<Temperature_Shift_Val;
		}

	}
	else
	{
		sendbuffer[4-1]|=Tem_Warn_Normal<<Temperature_Shift_Val;
	}




	if(SysPara.ChannleStatus.CurChannle_b==Channle_Status_Open) 
	{
		if(value->CurData.bVal/10>SysPara.thresholdVal.CurThreshold_b)
		{
			sendbuffer[5-1]|=Cur_Warn_OverCurrent<<Current_Shift_Val;
		}
		else  if(value->CurData.bVal>Current_Zero_Point_abc)
		{
			sendbuffer[5-1]|=Cur_Warn_Normal<<Current_Shift_Val;
			
		}
		else
		{
			sendbuffer[5-1]|=Cur_Warn_NoSensor<<Current_Shift_Val; 
		}		

	}
	else
	{
		sendbuffer[5-1]|=Cur_Warn_Normal<<Current_Shift_Val;
	}

	if(SysPara.ChannleStatus.VolChannle_b==Channle_Status_Open)
	{
		if(value->VolData.bVal>SysPara.thresholdVal.Vol_OverThreshold_b)
		{
			sendbuffer[5-1]|=Vol_Warn_OverVoltage<<Voltage_Shift_Val;
		}
		else	if(value->VolData.bVal>SysPara.thresholdVal.Vol_BelowThreshold_b)
		{
			sendbuffer[5-1]|=Vol_Warn_Normal<<Voltage_Shift_Val;
		}
		else	if( value->VolData.bVal>Voltage_Zero_Point)
		{
			sendbuffer[5-1]|=Vol_Warn_UnderVoltage<<Voltage_Shift_Val;
		}
		else 
		{
			sendbuffer[5-1]|=Vol_Warn_CutOFF<<Voltage_Shift_Val;
		}
	}
	else 
	{
		sendbuffer[5-1]|=Vol_Warn_Normal<<Voltage_Shift_Val;
	}

	if(SysPara.ChannleStatus.TemChannle_b==Channle_Status_Open) 
	{
		if( value->TemData.bVal/10>SysPara.thresholdVal.TemThreshold_b)
		{	
			sendbuffer[5-1]|=Tem_Warn_OverHigh<<Temperature_Shift_Val;
		}
		else	if( value->TemData.bVal>0)
		{
			sendbuffer[5-1]|=Tem_Warn_Normal<<Temperature_Shift_Val;
		}
		else
		{
			
			sendbuffer[5-1]|=Tem_Warn_NoSensor<<Temperature_Shift_Val;
		}

	}
	else
	{
		sendbuffer[5-1]|=Tem_Warn_Normal<<Temperature_Shift_Val;
	}



	if(SysPara.ChannleStatus.CurChannle_c==Channle_Status_Open) 
	{
		if(value->CurData.cVal>SysPara.thresholdVal.CurThreshold_c)
		{
			sendbuffer[6-1]|=Cur_Warn_OverCurrent<<Current_Shift_Val;
		}
		else  if(value->CurData.cVal>Current_Zero_Point_abc)
		{
			sendbuffer[6-1]|=Cur_Warn_Normal<<Current_Shift_Val;
			
		}
		else
		{
			sendbuffer[6-1]|=Cur_Warn_NoSensor<<Current_Shift_Val; 
		}		

	}
	else
	{
		sendbuffer[6-1]|=Cur_Warn_Normal<<Current_Shift_Val;
	}

	if(SysPara.ChannleStatus.VolChannle_c==Channle_Status_Open)
	{
		if(value->VolData.cVal>SysPara.thresholdVal.Vol_OverThreshold_c)
		{
			sendbuffer[6-1]|=Vol_Warn_OverVoltage<<Voltage_Shift_Val;
		}
		else	if(value->VolData.cVal>SysPara.thresholdVal.Vol_BelowThreshold_c)
		{
			sendbuffer[6-1]|=Vol_Warn_Normal<<Voltage_Shift_Val;
		}
		else	if( value->VolData.cVal>Voltage_Zero_Point)
		{
			sendbuffer[6-1]|=Vol_Warn_UnderVoltage<<Voltage_Shift_Val;
		}
		else 
		{
			sendbuffer[6-1]|=Vol_Warn_CutOFF<<Voltage_Shift_Val;
		}
	}
	else 
	{
		sendbuffer[6-1]|=Vol_Warn_Normal<<Voltage_Shift_Val;
	}

	if(SysPara.ChannleStatus.TemChannle_c==Channle_Status_Open) 
	{
		if( value->TemData.cVal/10>SysPara.thresholdVal.TemThreshold_c)
		{	
			sendbuffer[6-1]|=Tem_Warn_OverHigh<<Temperature_Shift_Val;
		}
		else	if( value->TemData.cVal>0)
		{
			sendbuffer[6-1]|=Tem_Warn_Normal<<Temperature_Shift_Val;
		}
		else
		{
			
			sendbuffer[6-1]|=Tem_Warn_NoSensor<<Temperature_Shift_Val;
		}

	}
	else
	{
		sendbuffer[6-1]|=Tem_Warn_Normal<<Temperature_Shift_Val;
	}

 

	if(SysPara.ChannleStatus.TemChannle_s==Channle_Status_Open) 
	{
		if(value->TemData.sVal/10>SysPara.thresholdVal.TemThreshold_s)
		{
			
			sendbuffer[7-1]|=Tem_Warn_OverHigh<<0;
		}
		else	if(value->TemData.sVal>0)
		{
			
			sendbuffer[7-1]|=Tem_Warn_Normal<<0;
		}
		else
		{
				
			sendbuffer[7-1]|=Tem_Warn_NoSensor<<0;
		}

	}
	else  
	{
		sendbuffer[7-1]|=Tem_Warn_Normal<<0;
	}



	

	if(SysPara.ChannleStatus.CurChannle_r==Channle_Status_Open) 
	{
		if(value->CurData.rVal>SysPara.thresholdVal.CurThreshold_r)
		{
			sendbuffer[7-1]|=Cur_Warn_OverCurrent<<2;
		}
		else  if(value->CurData.rVal>Current_Zero_Point_r)
		{
			
			sendbuffer[7-1]|= Cur_Warn_Normal<<2;
		}
		else
		{
			sendbuffer[7-1]|=Cur_Warn_NoSensor<<2;
		}		

	}
	else
	{
		sendbuffer[7-1]|=Cur_Warn_Normal<<2;
	}


	sendbuffer[8-1]=(uint8)((value->CurData.aVal&0xFF00)>>8);
	sendbuffer[9-1]=(uint8)((value->CurData.aVal&0x00FF)>>0);
	
	sendbuffer[10-1]=(uint8)((value->VolData.aVal&0xFF00)>>8);
	sendbuffer[11-1]=(uint8)((value->VolData.aVal&0x00FF)>>0);
	if(value->TemData.aVal>0)
	{
		sendbuffer[12-1]=0;
		sendbuffer[13-1]=(uint8)((value->TemData.aVal&0xFF00)>>8);
		sendbuffer[14-1]=(uint8)((value->TemData.aVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[12-1]=1;
		sendbuffer[13-1]=(uint8)(((-1)*value->TemData.aVal&0xFF00)>>8);
		sendbuffer[14-1]=(uint8)(((-1)*value->TemData.aVal&0x00FF)>>0);
	}

	
	sendbuffer[15-1]=(uint8)((value->CurData.bVal&0xFF00)>>8);
	sendbuffer[16-1]=(uint8)((value->CurData.bVal&0x00FF)>>0);
	
	sendbuffer[17-1]=(uint8)((value->VolData.bVal&0xFF00)>>8);
	sendbuffer[18-1]=(uint8)((value->VolData.bVal&0x00FF)>>0);
	if(value->TemData.bVal>0)
	{
		sendbuffer[19-1]=0;
		sendbuffer[20-1]=(uint8)((value->TemData.bVal&0xFF00)>>8);
		sendbuffer[21-1]=(uint8)((value->TemData.bVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[19-1]=1;
		sendbuffer[20-1]=(uint8)(((-1)*value->TemData.bVal&0xFF00)>>8);
		sendbuffer[21-1]=(uint8)(((-1)*value->TemData.bVal&0x00FF)>>0);
	}


	sendbuffer[22-1]=(uint8)((value->CurData.cVal&0xFF00)>>8);
	sendbuffer[23-1]=(uint8)((value->CurData.cVal&0x00FF)>>0);
	
	sendbuffer[24-1]=(uint8)((value->VolData.cVal&0xFF00)>>8);
	sendbuffer[25-1]=(uint8)((value->VolData.cVal&0x00FF)>>0);
	if(value->TemData.cVal>0)
	{
		sendbuffer[26-1]=0;
		sendbuffer[27-1]=(uint8)((value->TemData.cVal&0xFF00)>>8);
		sendbuffer[28-1]=(uint8)((value->TemData.cVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[26-1]=1;
		sendbuffer[27-1]=(uint8)(((-1)*value->TemData.cVal&0xFF00)>>8);
		sendbuffer[28-1]=(uint8)(((-1)*value->TemData.cVal&0x00FF)>>0);
	}



	if(value->TemData.sVal>0)
	{
		sendbuffer[29-1]=0;
		sendbuffer[30-1]=(uint8)((value->TemData.sVal&0xFF00)>>8);
		sendbuffer[31-1]=(uint8)((value->TemData.sVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[29-1]=1;
		sendbuffer[30-1]=(uint8)(((-1)*value->TemData.sVal&0xFF00)>>8);
		sendbuffer[31-1]=(uint8)(((-1)*value->TemData.sVal&0x00FF)>>0);
	}

	
	sendbuffer[32-1]=(uint8)((value->CurData.rVal&0xFF00)>>8);
	sendbuffer[33-1]=(uint8)((value->CurData.rVal&0x00FF)>>0);

	
	sendbuffer[34-1]=0;
	
	
	/*crc = SumCalculate(sendbuffer+IMEI_Len+ICCID_Len+1,35);
	sendbuffer[IMEI_Len+ICCID_Len+34] = (crc & 0xFF00) >> 8;
	sendbuffer[IMEI_Len+ICCID_Len+35] = crc & 0x00FF;*/

	#if 0
	j=0;
	for(i=IMEI_Len+ICCID_Len;i<IMEI_Len+ICCID_Len+37;i++)
	{
		sprintf(databuffer+j*2,"%02x",sendbuffer[i]);
		++j;
	}
	
	
	memcpy(sendbuffer,value->BC26_IMEI,IMEI_Len);
	memcpy(sendbuffer+IMEI_Len,value->BC26_ICCID,ICCID_Len);

	memcpy(sendbuffer+IMEI_Len+ICCID_Len,databuffer,37*2);
	sendbuffer[IMEI_Len+ICCID_Len+37*2]=0;
	
	printf("sedbuffer=%s\n",sendbuffer);
	#endif
	
	i = 0;
	
	while (++i < 3) 
	{
		Lora_Tx_Data(sendbuffer, sizeof(sendbuffer));
		errcode = JudegWhetherSendingSuccessful(); 
		if (errcode == ErrCode_Success||errcode == ErrCode_ReceData)
		{
			return	SuccessFull;
		}
		if (errcode == ErrCode_Busy) 
		{
			NB_Delay(5000);
		}
	}
	return	FAIL;

}


uint8 LoRaModeReset(void)
{
	LoRo_Uart_CleanData();
	LoRa_Uart_Send("AT+RESET\r\n", 10);
	
	if (strstr(gNB_ReceData->buf, "OK+RESET\r\n") != NULL)
	{
		printf("Lora  Reset\n");
		return SUCCESS;
	}
	else
	{
		printf("Lora Reset Fail\n");
		return ERROR;
	}
}

/*
*********************************************************************************************************
*	�� �� ��: Check_Lora_Join_Status
*	����˵��: ���lora�Ƿ�����(�ڷ�������֮ǰ)����������1������������0
*	�� �� ֵ: �Ƿ�����
*   ��    �ߣ�����
*   ��    �ڣ�2019-02-22
*********************************************************************************************************/
u8 Check_Lora_Join_Status(void)
{
	LoRo_Uart_CleanData();

	LoRa_Uart_Send("AT+JOIN?\r\n", 10);

	

	if (strstr(gNB_ReceData->buf, "OK+JOIN:02\r\n") != NULL)
	{
		return SUCCESS;
	}
	else
	{
		return ERROR;
	}
	LoRo_Uart_CleanData();
}

/*
*********************************************************************************************************
*	�� �� ��: Set_Lora_Join
*	����˵��: lora����
*	�� �� ֵ: �Ƿ�ɹ��������ɹ�����ֵ1��ʧ�ܷ���ֵ0
*   ��    �ߣ�����
*   ��    �ڣ�2019-02-22
*********************************************************************************************************/
uint8 LoRaJoinNet(void)
{
	u16 i;
	LoRaSleepMode(FALSE);

	if(GetLoraDEVEUI(collectData.LORA_DEVEUI)==SuccessFull)
	{
		collectData.GetDeviceID=1;
	}	
	
	i = 10;
	while (i--) 
	{
		LoRo_Uart_CleanData();
		LoRa_Uart_Send("AT+JOIN?\r\n", 10);
		NB_Delay(3000);
		if(gNB_ReceData->len>0)
		{
			gNB_ReceData->buf[gNB_ReceData->len]=0;
			printf("R--->>%s\n",gNB_ReceData->buf);		
			if (strstr(gNB_ReceData->buf, "OK+JOIN:02\r\n") != NULL) 
			{
				SetLoRaModeToWorkClassC();
				return SuccessFull;
			} 
			else
			{
				break;
			}	
		}	
	}
	LoRo_Uart_CleanData();
	LoRa_Uart_Send("AT+JOIN=01,05,0A\r\n", 18);
	i=4;
	while (i--) 
	{
		if(i==2)
		{
			LoRo_Uart_CleanData();
			LoRa_Uart_Send("AT+JOIN=01,05,0A\r\n", 18);
		}
		NB_Delay(10000);
		if(gNB_ReceData->len>0)
		{
			gNB_ReceData->buf[gNB_ReceData->len]=0;
			printf("R--->>%s\n",gNB_ReceData->buf);		
			if (strstr(gNB_ReceData->buf, "OK+JOINED") != NULL) 
			{
				printf("�����ɹ�\n");	
				SetLoRaModeToWorkClassC();
				return SuccessFull;
			} 

			LoRo_Uart_CleanData();
		}	
	}	
	return FAIL; 
}

uint8 LoRaSleepMode(uint8 Flag)
{
	uint8 i = 0;
	LoRo_Uart_CleanData();
	if (Flag == FALSE)
	{
		LoRa_Uart_Send("AT+SLEEP=00\r\n", 13);
	}
	else
	{
		LoRa_Uart_Send("AT+SLEEP=01\r\n", 13);
	}
	i = 10;
	while (i--)
	{
		NB_Delay(1000);
		if (gNB_ReceData->len)
		{
			gNB_ReceData->buf[gNB_ReceData->len]=0;
			printf("R--->>%s\n",gNB_ReceData->buf);
			if (strstr(gNB_ReceData->buf, "OK+SLEEP:01\r\n") != NULL) 
			{
				printf("sleep mode\n");
				if (Flag == FALSE)
				{
					LoRo_Uart_CleanData();
					LoRa_Uart_Send("AT+SLEEP=00\r\n", 13);
				}
				else
				{
					return TRUE;
				}
			}
			else if (strstr(gNB_ReceData->buf, "OK+SLEEP:00\r\n") != NULL) 
			{
				printf("no sleep mode\n");
				if (Flag == FALSE)
				{
					return TRUE;
				}
				else
				{
					LoRo_Uart_CleanData();
					LoRa_Uart_Send("AT+SLEEP=01\r\n", 13);
				}
			}
			else
			{
				LoRo_Uart_CleanData();
				if (Flag == FALSE)
				{
					LoRa_Uart_Send("AT+SLEEP=00\r\n", 13);
				}
				else
				{
					LoRa_Uart_Send("AT+SLEEP=01\r\n", 13);
				}
			}
		}
		else
		{
			if (i % 3 == 0) 
			{
				LoRo_Uart_CleanData();
				if (Flag == FALSE)
				{
					LoRa_Uart_Send("AT+SLEEP=00\r\n", 13);
				}
				else
				{
					LoRa_Uart_Send("AT+SLEEP=01\r\n", 13);
				}
			}
		}

		if (1 == i)
		{
			printf("SleepMode failed\n");
			break;
		}
	}

	return FALSE;
}



uint8	SendAckToServer(uint8	CmdCode)
{
	uint8 	i = 0, errcode, addsendtime = 0;
	uint16	crc=0;
	uint8 	sendbuffer[35] = {0xB0, 0XB0,0X00};
	uint8	datalen=0;

	if(CmdCode==0x03)
	{
		sendbuffer[2]=CmdCode;
		
		sendbuffer[3]=(uint8)(SysPara.thresholdVal.CurThreshold_a>>8);
		sendbuffer[4]=(uint8)(SysPara.thresholdVal.CurThreshold_a&0X00FF);

		sendbuffer[5]=(uint8)(SysPara.thresholdVal.CurThreshold_b>>8);
		sendbuffer[6]=(uint8)(SysPara.thresholdVal.CurThreshold_b&0X00FF);

		sendbuffer[7]=(uint8)(SysPara.thresholdVal.CurThreshold_c>>8);
		sendbuffer[8]=(uint8)(SysPara.thresholdVal.CurThreshold_c&0X00FF);

		sendbuffer[9]=(uint8)(SysPara.thresholdVal.CurThreshold_r>>8);
		sendbuffer[10]=(uint8)(SysPara.thresholdVal.CurThreshold_r&0X00FF);
		

		sendbuffer[11]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_a>>8);
		sendbuffer[12]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_a&0X00FF);
		
		sendbuffer[13]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_b>>8);
		sendbuffer[14]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_b&0X00FF);
		
		sendbuffer[15]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_c>>8);
		sendbuffer[16]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_c&0X00FF);

		sendbuffer[17]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_a>>8);
		sendbuffer[18]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_a&0X00FF);
		
		sendbuffer[19]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_b>>8);
		sendbuffer[20]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_b&0X00FF);
		
		sendbuffer[21]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_c>>8);
		sendbuffer[22]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_c&0X00FF);

		sendbuffer[23]=(uint8)(SysPara.thresholdVal.TemThreshold_a>>8);
		sendbuffer[24]=(uint8)(SysPara.thresholdVal.TemThreshold_a&0X00FF);
		
		sendbuffer[25]=(uint8)(SysPara.thresholdVal.TemThreshold_b>>8);
		sendbuffer[26]=(uint8)(SysPara.thresholdVal.TemThreshold_b&0X00FF);
		
		sendbuffer[27]=(uint8)(SysPara.thresholdVal.TemThreshold_c>>8);
		sendbuffer[28]=(uint8)(SysPara.thresholdVal.TemThreshold_c&0X00FF);	

		sendbuffer[29]=(uint8)(SysPara.thresholdVal.TemThreshold_s>>8);
		sendbuffer[30]=(uint8)(SysPara.thresholdVal.TemThreshold_s&0X00FF);	
		
		sendbuffer[31]=0;
		sendbuffer[32]=0;		
		datalen=33;
		
	}
	else if(CmdCode==0x05) 
	{
		sendbuffer[2]=CmdCode;
		
		sendbuffer[3]=((SysPara.ChannleStatus.CurChannle_a==Channle_Status_Open)?0x01:0x00);
		sendbuffer[4]=((SysPara.ChannleStatus.CurChannle_b==Channle_Status_Open)?0x01:0x00);
		sendbuffer[5]=((SysPara.ChannleStatus.CurChannle_c==Channle_Status_Open)?0x01:0x00);
		sendbuffer[6]=((SysPara.ChannleStatus.CurChannle_r==Channle_Status_Open)?0x01:0x00);

		sendbuffer[7]=((SysPara.ChannleStatus.VolChannle_a==Channle_Status_Open)?0x01:0x00);
		sendbuffer[8]=((SysPara.ChannleStatus.VolChannle_b==Channle_Status_Open)?0x01:0x00);
		sendbuffer[9]=((SysPara.ChannleStatus.VolChannle_c==Channle_Status_Open)?0x01:0x00);

		sendbuffer[10]=((SysPara.ChannleStatus.TemChannle_a==Channle_Status_Open)?0x01:0x00);
		sendbuffer[11]=((SysPara.ChannleStatus.TemChannle_b==Channle_Status_Open)?0x01:0x00);
		sendbuffer[12]=((SysPara.ChannleStatus.TemChannle_c==Channle_Status_Open)?0x01:0x00);
		sendbuffer[13]=((SysPara.ChannleStatus.TemChannle_s==Channle_Status_Open)?0x01:0x00);
		sendbuffer[14]=0;
		sendbuffer[15]=0;	

		datalen=16;
		
	}
	else 
	{	printf("CmdCode error\n");	
		return FALSE;
	}
	
	i = 0;
	addsendtime = 0;
	while (++i < 3 + addsendtime) 
	{
		Lora_Tx_Data(sendbuffer, datalen);
		errcode = JudegWhetherSendingSuccessful(); 
		if ((errcode == ErrCode_Success) || (errcode == ErrCode_ReceData))
		{
			break;
		}
		if (errcode == ErrCode_Busy) 
		{
			NB_Delay(4000);
			addsendtime = 1;
		}
	}

	if ((errcode == ErrCode_Success) || (errcode == ErrCode_ReceData))
	{
		return SUCCESS;	
	}
	return	ERROR;

}


UINT8	GetLoraDEVEUI(uint8		*deveuibuffer)
{
	uint8	i=0;
	while(i++<3)
	{
		LoRo_Uart_CleanData();
		LoRa_Uart_Send("AT+DEVEUI?\r\n", 12);
		NB_Delay(2000);
		if(gNB_ReceData->len>0)
		{
			gNB_ReceData->buf[gNB_ReceData->len]=0;
			printf("R--->>%s\n",gNB_ReceData->buf);		
			if (strstr(gNB_ReceData->buf, "OK+DEVEUI:") != NULL) 
			{
				
				UtilGetStringFlag(gNB_ReceData->buf,"OK+DEVEUI:","\r\n",deveuibuffer);
				deveuibuffer[16]=0;
				printf("DEVEUI--->>%s\n",deveuibuffer);	
				
				return SuccessFull;
			} 

		}	

	}
	
	return	FAIL;
}


void	CmdPorcess(uint8	*Recebuffer)
{
	switch (Recebuffer[2])
	{
	case 0x02:
		printf("Set Threshold Value\n");

		SysPara.thresholdVal.CurThreshold_a=Recebuffer[3]<<8|Recebuffer[4];
		SysPara.thresholdVal.CurThreshold_b=Recebuffer[5]<<8|Recebuffer[6];
		SysPara.thresholdVal.CurThreshold_c=Recebuffer[7]<<8|Recebuffer[8];
		SysPara.thresholdVal.CurThreshold_r=Recebuffer[9]<<8|Recebuffer[10];
		

		SysPara.thresholdVal.Vol_OverThreshold_a=Recebuffer[11]<<8|Recebuffer[12];
		SysPara.thresholdVal.Vol_OverThreshold_b=Recebuffer[13]<<8|Recebuffer[14];
		SysPara.thresholdVal.Vol_OverThreshold_c=Recebuffer[15]<<8|Recebuffer[16];

		SysPara.thresholdVal.Vol_BelowThreshold_a=Recebuffer[17]<<8|Recebuffer[18];
		SysPara.thresholdVal.Vol_BelowThreshold_b=Recebuffer[19]<<8|Recebuffer[20];
		SysPara.thresholdVal.Vol_BelowThreshold_c=Recebuffer[21]<<8|Recebuffer[22];

		SysPara.thresholdVal.TemThreshold_a=Recebuffer[23]<<8|Recebuffer[24];	
		SysPara.thresholdVal.TemThreshold_b=Recebuffer[25]<<8|Recebuffer[26];	
		SysPara.thresholdVal.TemThreshold_c=Recebuffer[27]<<8|Recebuffer[28];	
		SysPara.thresholdVal.TemThreshold_s=Recebuffer[29]<<8|Recebuffer[30];	

		printf("--->>>����ֵ\nC=%d ",SysPara.thresholdVal.CurThreshold_a);
		printf("%d ",SysPara.thresholdVal.CurThreshold_b);	
		printf("%d ",SysPara.thresholdVal.CurThreshold_c);	
		printf("%d\n",SysPara.thresholdVal.CurThreshold_r); 		
		
		printf("V=%d ",SysPara.thresholdVal.Vol_OverThreshold_a);
		  printf("%d ",SysPara.thresholdVal.Vol_OverThreshold_b);	
		 printf("%d\n",SysPara.thresholdVal.Vol_OverThreshold_c);	
		
		  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_a);
		  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_b);
		 printf("%d\n",SysPara.thresholdVal.Vol_BelowThreshold_c);
		
		printf("T=%d ",SysPara.thresholdVal.TemThreshold_a);
		printf("%d ",SysPara.thresholdVal.TemThreshold_b);
		printf("%d ",SysPara.thresholdVal.TemThreshold_c);
		printf("%d\n",SysPara.thresholdVal.TemThreshold_s);	

		
		printf("save data1111\n");
		SysPara.thresholdVal_UsedDefault=Not_UsedDefaultValue;
		SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));

		SysPara.sendThresholdValueToLcd=1;
		
		break;
	case 0x04:
		printf("Set Channle Enable\n");

		SysPara.ChannleStatus.CurChannle_a=((Recebuffer[3]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.CurChannle_b=((Recebuffer[4]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.CurChannle_c=((Recebuffer[5]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.CurChannle_r=((Recebuffer[6]==0x01)?Channle_Status_Open:Channle_Status_Close);

		SysPara.ChannleStatus.VolChannle_a=((Recebuffer[7]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.VolChannle_b=((Recebuffer[8]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.VolChannle_c=((Recebuffer[9]==0x01)?Channle_Status_Open:Channle_Status_Close);

		SysPara.ChannleStatus.TemChannle_a=((Recebuffer[10]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.TemChannle_b=((Recebuffer[11]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.TemChannle_c=((Recebuffer[12]==0x01)?Channle_Status_Open:Channle_Status_Close);
		SysPara.ChannleStatus.TemChannle_s=((Recebuffer[13]==0x01)?Channle_Status_Open:Channle_Status_Close);	


	
	
	printf("--->>>ͨ������\nC=%02x ",SysPara.ChannleStatus.CurChannle_a);
	printf("%02x ",SysPara.ChannleStatus.CurChannle_b);
	printf("%02x ",SysPara.ChannleStatus.CurChannle_c);
	printf("%02x\n",SysPara.ChannleStatus.CurChannle_r);
	
	printf("V=%02x ",SysPara.ChannleStatus.VolChannle_a);
	printf("%02x ",SysPara.ChannleStatus.VolChannle_b);
	printf("%02x\n",SysPara.ChannleStatus.VolChannle_c);
	
	printf("T=%02x ",SysPara.ChannleStatus.TemChannle_a);
	printf("%02x ",SysPara.ChannleStatus.TemChannle_b);
	printf("%02x ",SysPara.ChannleStatus.TemChannle_c);
	printf("%02x\n",SysPara.ChannleStatus.TemChannle_s);


		
		
		printf("save data2222\n");
		SysPara.ChannleStatus_UsedDefault=Not_UsedDefaultValue;
		SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));

		SysPara.sendChannleEnableValueToLcd=1;	
		break;
	default:
		break;
	}



}

